package com.it.aop;

import org.aopalliance.intercept.Joinpoint;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

import java.util.Arrays;

/**
 * @author agstar
 */
@Aspect
public class LogAspects {

    @Pointcut("execution(public int com.it.aop.MathCalculator*.*(..))")
    public void pointCut() {

    }

    /**
     * 在模板方法之前切入
     */
    @Before("pointCut()")
    public void logStart(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        System.out.println("@Before[" + joinPoint.getSignature().getName() + "]运行。。。参数列表是。。{" + Arrays.asList(args) + "}");
    }

    @After("com.it.aop.LogAspects.pointCut()")
    public void logEnd(JoinPoint joinPoint) {
        System.out.println("@After结束。。。{}");
    }
    @Around("pointCut()")
    public Object logAround(ProceedingJoinPoint joinpoint) throws Throwable {
        System.out.println("@Around before");
        Object proceed = joinpoint.proceed();
        System.out.println("@Around after");
        return proceed;
    }

    @AfterReturning(value = "pointCut()", returning = "result")
    public void logReturn(JoinPoint joinPoint, Object result) {
        System.out.println("@AfterReturning正常返回。。。运行结果。。{" + result + "}");
    }

    @AfterThrowing(value = "pointCut()", throwing = "exception")
    public void logException(JoinPoint joinPoint,Exception exception) {
        System.out.println("@AfterThrowing异常。。。异常信息。。{}");
        exception.printStackTrace();
    }


}
